package com.bucg.cjcloud.gb28181.controller;

import com.bucg.cjcloud.gb28181.common.entity.Device;
import com.bucg.cjcloud.gb28181.common.entity.ItemEntity;
import com.bucg.cjcloud.gb28181.common.entity.PlatformEntity;
import com.bucg.cjcloud.gb28181.common.service.DeviceMangeService;
import com.bucg.cjcloud.gb28181.media.model.ResponseResult;
import com.bucg.cjcloud.gb28181.sip.RequestMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SipController {
    @Autowired
    DeviceMangeService deviceMangeService;

    @Autowired
    RequestMessageService rms;


    @GetMapping("/sip")
    public ResponseEntity ctrl(String deviceId, String method) {

        ResponseResult s = sendRequest(method, deviceId, false);
        return new ResponseEntity<>(s, HttpStatus.OK);


    }

    public ResponseResult sendRequest(String method, String deviceId, boolean rePlay) {
        Device device = new Device(null, deviceId, null);

        //logger.info(">>> sendRequest method=" + method + ", device=" + device);
        String type = "ok";
        String result = "success";
        try {

            switch (method.toUpperCase()) {
                case "PLAY":
                    result = rms.play(device,rePlay);
                    break;
                case "CATALOG":
                    rms.catalog(device);
                    break;
                case "INFO":
                    rms.deviceInfo(device);
                    break;
                // case "STATUS":
                //     rms.status(device);
                //     break;
                // case "CTRLUP":
                //     rms.ctrlUp(device);
                //     break;
                // case "BOOT":
                //     rms.boot(device);
                // case "CTRLSTOP":
                //     rms.ctrlStop(device);
                case "BYE":
                    rms.bye(device);
                    break;
                default:
                    type = "error";
                    result = "no operation";
            }
        } catch (Exception e) {
            e.printStackTrace();
            type = "error";
            result = "failed! reseaon: " + e.getMessage();
        }
        return new ResponseResult(type, result);
    }

    @GetMapping("/items")
    public ResponseEntity<List<ItemEntity>> getItems(
            @RequestParam(required = false) Long platformId,
            @RequestParam(required = false) String pDeviceId,
            @RequestParam(required = false) String deviceId) {
        return ResponseEntity.ok(deviceMangeService.getItems(platformId, pDeviceId, deviceId));

    }

    @GetMapping("/platforms")
    public ResponseEntity<List<PlatformEntity>> getPlatforms(
            @RequestParam(required = false) Long platformId,
            @RequestParam(required = false) String pDeviceId
    ) {

        return ResponseEntity.ok(deviceMangeService.getPlatForms(platformId, pDeviceId));
    }
}

